package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;

import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.qiantu.api.entity.MemberBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.aop.SingleClick;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.dialog.BottomEditDialog;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;
import com.lxj.xpopup.XPopup;

import okhttp3.Call;

public class MemberInfoActivity extends AppActivity {
    private MemberBean memberBean;
    private TextView nickName;
    private TextView phone;
    private TextView remark;
    private View remarkNext;
    private TextView btnGly;
    private TextView btnMember;
    private LinearLayout permissionLayout;
    private TextView delete;
    private ConfirmCancelHintDialog mConfirmCancelHintDialog;
    @Log
    public static void start(Context context, MemberBean memberBean) {
        Intent intent = new Intent(context, MemberInfoActivity.class);
        intent.putExtra("memberBean", memberBean);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_member_info;
    }

    @Override
    protected void initView() {
        nickName = (TextView) findViewById(R.id.nick_name);
        phone = (TextView) findViewById(R.id.phone);
        remark = (TextView) findViewById(R.id.remark);
        remarkNext = findViewById(R.id.remark_next);
        btnGly = findViewById(R.id.btn_guanliyuan);
        btnMember = findViewById(R.id.btn_member);
        permissionLayout = findViewById(R.id.room_permission);
        delete = (TextView) findViewById(R.id.delete);
        setOnClickListener(R.id.remark_btn, R.id.delete,R.id.btn_guanliyuan,R.id.btn_member,R.id.room_permission);
    }

    @Override
    protected void initData() {
        memberBean = (MemberBean) getIntent().getSerializableExtra("memberBean");
        if (AppApplication.getInstance().getCurHouseInfo().getRoleType() == 0){
            remarkNext.setVisibility(View.VISIBLE);
            delete.setVisibility(View.VISIBLE);
        }else {
            remarkNext.setVisibility(View.GONE);
            delete.setVisibility(View.GONE);
            btnGly.setVisibility(View.GONE);
            btnMember.setVisibility(View.GONE);
            permissionLayout.setVisibility(View.GONE);
            if (AppApplication.getInstance().getCurHouseInfo().getRoleType() == 2){
                findViewById(R.id.room_permission_layout).setVisibility(View.GONE);
            }
        }
        nickName.setText(memberBean.getUserNickName());
        phone.setText(memberBean.getUserPhone());
        remark.setText(memberBean.getMemberNickName());
        updateView();
    }

    private void updateView(){
        if(memberBean.getRoleType() == 1){
            btnGly.setSelected(true);
            btnMember.setSelected(false);
            permissionLayout.setVisibility(View.GONE);
        }else if(memberBean.getRoleType() == 2){
            btnGly.setSelected(false);
            btnMember.setSelected(true);
            permissionLayout.setVisibility(View.VISIBLE);
        }
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.remark_btn) {
            if (remarkNext.getVisibility() == View.VISIBLE){
                showBottomEditDialog(memberBean.getMemberNickName());
            }
        } else if (id == R.id.delete){
            showConfirmCancelHintDialog(getString(R.string.delete_house_member_hint), "取消", "删除", new ConfirmCancelHintDialog.OnListener() {
                @Override
                public void onLeftClick(BaseDialog dialog) {
                    dialog.dismiss();
                }

                @Override
                public void onRightClick(BaseDialog dialog) {
                    dialog.dismiss();
                    LLHttpManager.deletemember(MemberInfoActivity.this, AppApplication.getInstance().getCurrentHouseSerialNo(), memberBean.getUserSerialNo(), new HttpCallback<HttpData<Void>>(MemberInfoActivity.this){
                        @Override
                        public void onSucceed(HttpData<Void> result) {
                            super.onSucceed(result);
                            finish();
                        }
                    });
                }
            });
        }else if(id == R.id.room_permission){
            Intent intent = new Intent(this,MemberRoomActivity.class);
            intent.putExtra("type",2);
            intent.putExtra("data",memberBean);
            startActivityForResult(intent,new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (resultCode == RESULT_OK){
                        memberBean = (MemberBean) data.getSerializableExtra("data");
                    }
                }
            });
        }else if(id == R.id.btn_guanliyuan){
            if(memberBean.getRoleType() == 2){
                showConfirmDialog(1);
            }
        }else if(id == R.id.btn_member){
            if(memberBean.getRoleType() == 1){
                showConfirmDialog(2);
            }
        }
    }

    private BottomEditDialog mBottomEditDialog;
    private void showBottomEditDialog(String text) {
        if (mBottomEditDialog == null) {
            mBottomEditDialog = new BottomEditDialog(this);
            mBottomEditDialog.setDialogClickListener(new BottomEditDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {
                    eidtMemberNickname(mBottomEditDialog.getText());
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomEditDialog);
        }
        mBottomEditDialog.setLabel(getString(R.string.remark));
        mBottomEditDialog.setBtnText(getString(R.string.save));
        mBottomEditDialog.setText(text);
        mBottomEditDialog.setMaxLength(16);
        mBottomEditDialog.show();
    }

    private void showConfirmDialog(int roleType){
        String message = "";
        if(roleType == 1){
            message = "升至管理员后，他/她将能够控制和编辑房屋所有内容，确定继续嘛？";
        }else{
            message = "降至普通成员后，他/她将只能控制获得权限的房间的场景/设备，确定继续嘛？";
        }
        showConfirmCancelHintDialog(message, getString(R.string.cancel), getString(R.string.ok), new ConfirmCancelHintDialog.OnListener() {
            @Override
            public void onLeftClick(BaseDialog dialog) {
                dialog.dismiss();
            }

            @Override
            public void onRightClick(BaseDialog dialog) {
                dialog.dismiss();
                changeRole(roleType);
            }
        });
    }

    private void eidtMemberNickname(String nikeName) {
        LLHttpManager.eidtMemberNickname(getAppActivity(), memberBean.getUserSerialNo(), memberBean.getHouseSerialNo(), nikeName, new HttpCallback<HttpData<Void>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                memberBean.setMemberNickName(nikeName);
                remark.setText(memberBean.getMemberNickName());
            }
        });
    }

    private void changeRole(int roleType) {
        LLHttpManager.changeRole(getAppActivity(), memberBean.getHouseSerialNo(), memberBean.getUserSerialNo(), roleType, new HttpCallback<HttpData<Void>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                memberBean.setRoleType(roleType);
                updateView();
            }
        });
    }
}